let db = wx.cloud.database({
  env: 'mini-cloud-8gonte8a94b9dcfb'
});
let poemsl = db.collection('db_poemsl');
let author = db.collection('db_authors');
let collect = db.collection('db_collect');
let history = db.collection('db_history');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      pid:'',
      arr:[],
      author:[],
      iscollect:false
  },
  backUrl(){
    wx.navigateBack()
  },
  cancelCollect(){
    let poem_id=this.data.arr._id
    let user_id=wx.getStorageSync('openid')
    collect.where({
      poem_id,
      user_id
    }).remove().then(res=>{
      this.setData({
        iscollect:false
      })
      wx.showToast({
        title: '取消收藏',
        icon:"success"
      })
    })
  },
  collectPoetry(){
    let id=wx.getStorageSync('openid')
    if(id.length==0){
      wx.showModal({
        title: '还未登录',
        content: '是否前往个人页登录？',
        success(res){
          if (res.confirm) {
              wx.switchTab({
                url: `../mine/mine`,
              })
          } else if (res.cancel) {
            return
          }
        }
      })
    }else{
      let user_id=id
      let poem_id=this.data.arr._id
      let poem_name=this.data.arr.name
      let author_name=this.data.arr.author
      let poem_dynasty=this.data.arr.dynasty
      collect.add({data:{
        user_id,
        poem_id,
        poem_name,
        author_name,
        poem_dynasty
      }}).then(res => {
        this.setData({
          iscollect:true
        })
        wx.showToast({
          title: '收藏成功',
          icon:"success"
        })
       
       })
      .catch(err => { //添加失败
        console.log('添加失败',err)
       })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      wx.showLoading({
            title: "正在加载...",
            icon: "none",
      });
      let that = this
      
     that.setData({
       pid:options.id
     })
     poemsl.where({
       _id:that.data.pid
     }).get().then(content=>{
       let {data,errMsg}=content
       that.setData({
         arr:data[0]
       })
      //  console.log(that.data.arr);
       author.where({
         name:that.data.arr.author
       }).get().then(cont=>{
         let {data,errMsg}=cont
         that.setData({
          author:data[0]
        })
       
       })

       //判断用户是否登录
       let user_id=wx.getStorageSync('openid')
       if(user_id==0){
        wx.hideLoading();
         return
       }else{
        //用户已经登录，判断是否收藏
        collect.where({
          user_id,
          poem_id:this.data.arr._id
        }).get().then(res=>{
          // console.log(res.data.length);
          if(res.data.length!=0){
            this.setData({
              iscollect:true
            })
          }else{
            this.setData({
              iscollect:false
            })
          }
          //添加历史记录
          let user_id=wx.getStorageSync('openid')
          let poem_id=this.data.arr._id
          let poem_name=this.data.arr.name
          let author_name=this.data.arr.author
          let poem_dynasty=this.data.arr.dynasty
          console.log(user_id,poem_id);
          history.where({
            poem_id,
            user_id
          }).get().then(res=>{
            console.log(res);
            if(res.data.length==0){
              history.add({
                data:{
                  user_id,
                  poem_id,
                  poem_name,
                  author_name,
                  poem_dynasty
                }
              }).then(res=>{
                console.log(res);
              })
            }else{
              history.where({
                poem_id,
                user_id
              }).remove().then(res=>{
                history.add({
                  data:{
                    user_id,
                    poem_id,
                    poem_name,
                    author_name,
                    poem_dynasty
                  }
                }).then(res=>{
                  console.log(res);
                })
              })
            }
            wx.hideLoading();
          })

        })
       }
     })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})